package com.flycms.web.front.rest;

import com.flycms.core.entity.RestDataVo;
import com.flycms.core.entity.RestStatusCode;
import com.flycms.module.agent.UserAgentUtils;
import com.flycms.module.article.service.ArticleRestService;
import com.flycms.module.qasystem.service.AnswersService;
import com.flycms.module.qasystem.service.QuestionsService;
import com.flycms.module.soft.service.SoftApplyService;
import com.flycms.module.system.model.System;
import com.flycms.module.system.service.SystemService;
import com.flycms.module.user.service.UserRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 数据中心接口，模拟获取注册用户，访问次数，社区活跃数、解决方案、帮助文档等统计
 * @author huangzt
 * @version 1.0
 * @date 2020/3/1 12:42
 */
@RestController
public class DataCenterController {

    @Autowired
    private ArticleRestService articleService;
    @Autowired
    private UserRestService userService;
    @Autowired
    private QuestionsService questionsService;
    @Autowired
    private AnswersService answersService;
    @Autowired
    private SoftApplyService softApplyService;
    @Autowired
    private SystemService systemService;

    @GetMapping(value = "/countSolutionPage")
    public void countSolution(){
        UserAgentUtils.solutionCount.incrementAndGet();
    }

    @GetMapping(value = "/getDataCenter")
    public RestDataVo getData() throws Exception{
        RestDataVo restDataVo = new RestDataVo();
        Map<String,Object> map = new HashMap<>(16);
        //查询通过审核的文章总数
        int articleCount = articleService.getArticleIndexCount();
        //查询问题总数
        long questionCount = questionsService.getQuestionCount(null,null,null);
        //查询回答总数
        long answersCount = answersService.getAnswerCount(null,null,null);
        //查询注册用户总数
        int userCount = userService.getUserCount();
        ConcurrentHashMap<String, Long> visitViewMap = UserAgentUtils.visitViewMap;
        //查询访客总数
        long uniqueVisitorCount = visitViewMap.get("unique_visitor");
        //查询总访问量
        long pageViewCount = visitViewMap.get("page_view");
        //申请总数
        long applyCount = softApplyService.getSoftApplyCount(null);

        map.put("articleCount",articleCount);
        map.put("questionCount",questionCount);
        map.put("answersCount",answersCount);
        map.put("userCount",userCount);
        map.put("uniqueVisitorCount",uniqueVisitorCount);
        map.put("pageViewCount",pageViewCount);
        map.put("applyCount",applyCount);
        //解决方案浏览量
        map.put("solutionCount",UserAgentUtils.solutionCount.longValue());
        restDataVo.setCode(RestStatusCode.SUCCESS.getStatusCode());
        restDataVo.setMsg("查询成功");
        restDataVo.setData(map);
        return restDataVo;
    }


    @GetMapping(value = "/getPhoneAndEmail")
    public RestDataVo getPhoneAndEmail(){
        RestDataVo restDataVo = new RestDataVo();
        System system = systemService.getSystem();
        restDataVo.setData(system);
        restDataVo.setCode(200);
        restDataVo.setMsg("成功");
        return restDataVo;
    }

}
